/*
 * =====================================================================================
 *
 *       Filename:  map.cpp
 *
 *    Description:  j
 *
 *        Version:  1.0
 *        Created:  22.01.2010 12:16:41
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Michal Antonczak (uzyszkodnik), uzyszkodnik.priv@gmail.com
 *        Company:  
 *
 * =====================================================================================
 */

#include 	"map.h"
#include	"gamestructs.h"
#include	"sdlstuff.h"
#include	<confuse.h>
#include	<string.h>
#include	<SDL/SDL.h>
#include	<SDL/SDL_rotozoom.h>

using namespace std;

Game_Map::Game_Map(string filepath){

	string map_path, config_path, collision_map_path;

	filepath = filepath + '/';
	map_path = filepath+"map.png";
	collision_map_path = filepath+"collision.png";

	config_path = filepath+"config";


	img = load_img(map_path.c_str());
	collision_map = load_img(collision_map_path.c_str());

	readconfig(config_path.c_str());	

	posi.x = img->w;
	posi.y = img->h;

}

void Game_Map::readconfig(const char* path){
	double gravity;
	cfg_opt_t opts[]={
		CFG_SIMPLE_FLOAT("engine_inc",&ship_info.engine_inc),
		CFG_SIMPLE_FLOAT("ship_mass",&ship_info.ship_mass),
		CFG_SIMPLE_FLOAT("fuel_usage", &ship_info.fuel_usage),
		CFG_SIMPLE_FLOAT("fuel_mass", &ship_info.fuel_mass),
		CFG_SIMPLE_FLOAT("engine_max_power", &ship_info.engine_max_power),
		CFG_SIMPLE_FLOAT("rotation_speed", &ship_info.rotation_speed),
		CFG_SIMPLE_FLOAT("gravity", &gravity),
		CFG_SIMPLE_FLOAT("atmospheric_rub", &phys.rub),
		CFG_SIMPLE_FLOAT("safe_landing_angle", &ship_info.safe_landing_angle),
		CFG_SIMPLE_FLOAT("safe_landing_speed_y", &ship_info.safe_landing_speed.y),
		CFG_SIMPLE_FLOAT("safe_landing_speed_x", &ship_info.safe_landing_speed.x),
		CFG_SIMPLE_INT("start_x", &ship_info.start_x),
		CFG_SIMPLE_INT("start_y", &ship_info.start_y),
		CFG_END()
	};
	cfg_t* cfg;
	cfg= cfg_init(opts,0);
	cfg_parse(cfg,path);

	phys.gravity.setval(gravity,0);

	ship_info.max_fuel_mass = ship_info.fuel_mass;		

}

Game_Map::~Game_Map(){
	SDL_FreeSurface(img);
	SDL_FreeSurface(collision_map);
}


/*
Short_map_info::Short_map_info(string path){

	//Game_Map* tmpmap;
	SDL_Surface* tmpmap;

	name = path;

	path+="/map.png";

	int minimalize;
	tmpmap = load_img(path.c_str());

	// how much do we want to minimalize the surface to match the full screen

	minimalize = 600/tmpma->h;

	if(tmpmap->w*minimalize>800)
		minimalize = 800/tmpmap->w; 

	miniature = rotozoomSurface(tmpmap->img,0, minimalize,1);

	SDL_FreeSurface(tmpmap);


}
*/

Short_map_info::Short_map_info(TTF_Font* mapname_font, string path){
	font = mapname_font;

	//Game_Map* tmpmap;
	SDL_Surface* tmpmap;
	SDL_Surface *minitmp,*miniaturetmp,*tmp;
	SDL_Color fontColor = {255,255,255};
	SDL_Rect pos, name_pos;
//	pos.x=10;
//	pos.y = 10;
	
	name_pos.x = 0 ;
	name_pos.y = 0 ;

	name_str = path;
	
	name = TTF_RenderText_Solid( font, path.c_str(), fontColor ); 

	path+="/map.png";

	double tmpx,tmpy;


	tmpmap = load_img(path.c_str());

	// how much do we want to minimalize the surface to match the full screen
	tmpx=800.0f/(double)(tmpmap->w);
	tmpy=600.0f/(double)(tmpmap->h);


//	if(tmpy<tmpx)
//		miniaturetmp = rotozoomSurface(tmpmap,0,tmpy,1);
//	else
//		miniaturetmp = rotozoomSurface(tmpmap,0,tmpx,1);
//
	
	
	tmpy = 300.0f/(double)(tmpmap->h);


	if(tmpy<tmpx)
		minitmp = rotozoomSurface(tmpmap,0,tmpy,1);
	else
		minitmp = rotozoomSurface(tmpmap,0,tmpx,1);



	tmp = SDL_CreateRGBSurface(SDL_HWSURFACE, minitmp->w, minitmp->h+20+name->h,32 ,0, 0, 0,0);
	
	pos.x = 0;
	pos.y = 20+name->h;

	name_pos.y = 0;
	name_pos.x = (tmp->w - name->w ) / 2;


	SDL_BlitSurface(minitmp, NULL, tmp , &pos)	;
	SDL_BlitSurface(name, NULL, tmp , &name_pos);

	mini = tmp;

	SDL_FreeSurface(minitmp);
	SDL_FreeSurface(tmpmap);
	SDL_FreeSurface(name);
//	SDL_FreeSurface(miniaturetmp);

}
/* 
void Short_map_info::load(string path){

	//Game_Map* tmpmap;
	SDL_Surface* tmpmap;

	SDL_Color fontColor = {255,255,255};

	name_str = path;

	path+="/map.png";

	double tmpx,tmpy;

	puts(path.c_str());

	tmpmap = load_img(path.c_str());

	// how much do we want to minimalize the surface to match the full screen
	tmpx=800.0f/(double)(tmpmap->w);
	tmpy=600.0f/(double)(tmpmap->h);

	name = TTF_RenderText_Solid( font, path.c_str(), fontColor ); 

	if(tmpy>tmpx)
		miniature = rotozoomSurface(tmpmap,0,tmpy,1);
	else
		miniature = rotozoomSurface(tmpmap,0,tmpx,1);

	SDL_FreeSurface(tmpmap);



}
*/

Short_map_info::~Short_map_info(){

//	SDL_FreeSurface(miniature);
	SDL_FreeSurface(mini);
//	SDL_FreeSurface(name);
}
